//
//  SetTextViewController.m
//  leo
//
//  Created by LIU YU LIN on 15/11/25.
//  Copyright © 2015年 mdx. All rights reserved.
//

#import "SetTextViewController.h"
#import "SyncManager.h"
#import "LeoMsg.h"
#import "RelationShipViewController.h"
@interface SetTextViewController ()

@end

@implementation SetTextViewController
@synthesize confirmButton;
@synthesize cancelButton;
@synthesize titleLabel;
@synthesize contentTextField;
@synthesize popUpView;
@synthesize motherWindow;
@synthesize motherLabel;
@synthesize tipsString;
@synthesize initialString;
@synthesize initialTitle;
@synthesize fatherVC;

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
    [contentTextField resignFirstResponder];
}

-(void)viewWillAppear:(BOOL)animated
{
    if (tipsString!=NULL)
    {
        self.titleLabel.text=tipsString;
    }
    if (initialString!=NULL)
    {
        self.contentTextField.text=initialString;
    }
    if (initialTitle!=NULL)
    {
        self.titleLabel.text=initialTitle;
    }
}
-(void)dealloc
{
    motherWindow=NULL;
    motherLabel=NULL;
    tipsString=NULL;
    initialString=NULL;
    initialTitle=NULL;
    fatherVC=NULL;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    [self setBackGroudView];
    [self initButtonActions];
    contentTextField.delegate=self;
    // Do any additional setup after loading the view from its nib.
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
    if(self.view.window==nil)
    {
        self.view=nil;
    }
}

- (void)setBackGroudView{
    [self.view setFrame:[UIScreen mainScreen].bounds];
    [self.view setBackgroundColor:[UIColor colorWithWhite:0.0 alpha:0.4]];
    self.popUpView.layer.cornerRadius = 8;
}

-(void)initButtonActions{
    
    [confirmButton addTarget:self action:@selector(submitAction) forControlEvents:UIControlEventTouchDown];
    [cancelButton addTarget:self action:@selector(cancelAction) forControlEvents:UIControlEventTouchDown];
    
    if(self.type==2)
    {
        if (fatherVC!=nil)
        {
            RelationShipViewController *relation=(RelationShipViewController *)fatherVC;
            //relation.novaRelationShip=contentTextField.text;
            [confirmButton addTarget:relation action:@selector(addRelationAction) forControlEvents:UIControlEventTouchDown];
        }
        
    }
    [contentTextField addTarget:self action:@selector(nickTextFieldChange:) forControlEvents:UIControlEventEditingChanged];

}
- (void)nickTextFieldChange:(UITextField *)textField
{
    if (textField.text.length > 20) {
        textField.text = [textField.text substringToIndex:20];
    }
}
-(void)submitAction
{
//    
//    if (contentTextField.text.length>10)
//    {
//        [LeoMsg showMsg:@"输入不能超过10个字符!"];
//        return;
//    }
    [contentTextField resignFirstResponder];

    if (self.type==2&&[Tooler checkStringIllegal:contentTextField.text ChnLength:6 EngLength:10]==2)
    {
        [LeoMsg showToastMsg:@"关系名称长度过长" parentView:self.motherWindow showPosition:TOAST_SHOW_END hideAfterDelay:1];
        return;
    }
    if (self.type==1&&(contentTextField.text==NULL||contentTextField.text.length==0))
    {
        [LeoMsg showToastMsg:@"昵称不能为空" parentView:self.motherWindow showPosition:TOAST_SHOW_END hideAfterDelay:1];
        return;
    }
    if(self.type==1&&[Tooler checkStringIllegal:contentTextField.text ChnLength:6 EngLength:10]==1)
    {
        [LeoMsg showToastMsg:@"昵称不合法" parentView:self.motherWindow showPosition:TOAST_SHOW_END hideAfterDelay:1];
        return;
    }

    if(self.type==1&&[Tooler checkStringIllegal:contentTextField.text ChnLength:6 EngLength:10]==1)
    {
        [LeoMsg showToastMsg:@"昵称不合法" parentView:self.motherWindow showPosition:TOAST_SHOW_END hideAfterDelay:1];
        return;
    }
    if(self.type==1&&[Tooler checkStringIllegal:contentTextField.text ChnLength:6 EngLength:10]==2)
    {
        [LeoMsg showToastMsg:@"昵称太长，中文字符只能输入6个，英文字符最多10个" parentView:self.motherWindow showPosition:TOAST_SHOW_END hideAfterDelay:1];
        return;
    }

    
    if((contentTextField.text==NULL)&&(self.type!=3))
    {
        [LeoMsg showMsg:@"输入不能为空"];
        return;
    }

    //TODO:
    self.motherWindow.hidden=YES;
    if (([contentTextField.text  isEqual:@""])&&(self.type!=3))
    {
        [LeoMsg showMsg:@"输入不能为空"];
        return;
    }
    self.motherLabel.text=contentTextField.text;
    if(self.type==1)
    {
        SyncManager *syncManager=[[SyncManager alloc] init];
        if(syncManager==NULL)
            return;
        User *user=[User sharedSingleton];
        if(user==NULL)
            return;
        user.nickName=self.motherLabel.text;
        [syncManager setUserInfo1:^(SyncResult result, NSDictionary *retValue, NSString *description) {
           if(result==SUCESS)
               [LeoMsg showMsg:@"昵称修改成功"];
            else
            {
                [Tooler showHttpMsg:result description:description msg:@"昵称修改失败"];
            }
        }];
    }
    if(self.type==2)
    {
        if (fatherVC!=nil)
        {
            RelationShipViewController *relation=(RelationShipViewController *)fatherVC;
            relation.novaRelationShip=contentTextField.text;

        }

    }
    
}

-(void)cancelAction
{
    self.motherWindow.hidden=YES;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

//***键盘遮挡部分
//开始编辑输入框的时候，软键盘出现，执行此事件
-(void)textFieldDidBeginEditing:(UITextField *)textField
{
    CGFloat kbHeight =258;
    int INTERVAL_KEYBOARD=258;
    
    CGFloat offset =self.popUpView.frame.origin.y+textField.frame.origin.y + 125/2 +64- (self.view.frame.size.height - kbHeight);//键盘高度300
    
    NSLog(@"%f, %f",self.popUpView.frame.origin.y,self.view.frame.size.height);
    //CGFloat offset = (textField.frame.origin.y+textField.frame.size.height+INTERVAL_KEYBOARD) - (self.view.frame.size.height - kbHeight);
    
    
    NSTimeInterval animationDuration = 0.30f;
    [UIView beginAnimations:@"ResizeForKeyboard" context:nil];
    [UIView setAnimationDuration:animationDuration];
    NSLog(@"%f",offset);
    
    //将视图的Y坐标向上移动offset个单位，以使下面腾出地方用于软键盘的显示
    if(offset > 0)
        self.view.frame = CGRectMake(0.0f, -offset, self.view.frame.size.width, self.view.frame.size.height);
    
    [UIView commitAnimations];
}

//当用户按下return键或者按回车键，keyboard消失
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}

//输入框编辑完成以后，将视图恢复到原始状态
-(void)textFieldDidEndEditing:(UITextField *)textField
{
    self.view.frame =CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height);
    [textField resignFirstResponder];
}

@end
